Skip to content

[executorch][native] Keep float scales of int4 ggml embeddings exact - #25049

Open
pytorchbot wants to merge 1 commit into
gh/digantdesai/203/origfrom
gh/digantdesai/204/orig
Open

pytorchbot wants to merge 1 commit into
gh/digantdesai/203/origfrom
gh/digantdesai/204/orig

Conversation

@pytorchbot

Copy link
Copy Markdown
Collaborator

This PR was created by the merge bot to help merge the original PR into the main branch.
ghstack PR number: #24975 by @digantdesai
^ Please use this as the source of truth for the PR details, comments, and reviews
ghstack PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/204/base
ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/204/head
Merge bot PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/203/orig
Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/204/orig
Differential Revision: D130869557
@diff-train-skip-merge

Pull Request resolved: #24975

An int4 weight-only embedding stores its group scales in the PTN as f32 or bf16, and eager dequantizes with those exact values. ggml's Q4_0 blocks hold only an fp16 scale, so the ggml engine rounded each scale to fp16 at load, and 22 of the 219 embedding tests missed eager on CPU and CUDA: `embedding_int4wo_g128.decode_f32`, for one, is off by an RMSE of 1.8e-4 against a tolerance of 1e-5. The PTN carries the exact scales, so the engine was wrong, not the test, and a looser tolerance would hide that. Q4_0 can't take f32 scales without a ggml change, and dequantizing the table to f32 at load would match eager at about 7 times the memory.

A linear's matmul reads the scales from inside the blocks, but an embedding only gathers rows and can scale them afterwards. An int4 constant that only `aten.embedding` reads, with a scale that an fp16 round trip changes, now loads as Q4_0 blocks with unit scales, next to an f32 tensor of its group scales. `aten.embedding` gathers rows from both and multiplies them, which matches eager exactly. The f32 scales cost 1 bit per weight at group size 32 and a quarter bit at 128.

Every other int4 weight loads as before, with fp16 scales in its blocks:
- a weight shared with a linear, as in tied embeddings;
- a mutable weight, because an op can rewrite its blocks;
- a weight whose scales are all fp16 values, so most bf16 tables stay a single tensor.

`q4_affine` now also rejects zero-width int4 weights, which used to fail later as an allocation error. ggml itself is unchanged.

Both ggml-CPU CI targets now run the 22 int4 weight-only f32 embedding tests that the previous diff excluded.

Authored with Claude Code.

Differential Revision: [D130869557](https://our.internmc.facebook.com/intern/diff/D130869557/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D130869557/)!
ghstack-source-id: 472753706
@pytorch-bot

pytorch-bot Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/25049

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 9eaa1d4 with merge base fcc9575 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 13, 2026

This branch was previously deployed

1 inactive deployment
cadence — 9eaa1d41 Deployed Nov 13, 2026 by pytorchbot via hifi5s-op-test / hifi5s #38271
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants